import { _polar, translateY, _random, plainMatrix } from "../../math/index.js"
import { polarPoints } from "../../math/points/polarPoints.js";
import { pointsPath } from "../path.js";
import { _colors } from '../../color/index.js'
import { _g } from "../svg.js";
export const treeCurveTypes = ['QQ', 'QL', 'V', 'LL']

export const treePoints = (options) => {
    let { o = [300, 200], n = 3, r = 100, a = -90, depth = 1, k = 2 / 3, random = 10, treeCurve = 'QQ' } = options
    let result = new Array(depth)
    let _ra = () => {
        return random ? _random(-random, random) : 0
    }
    let _trer = (o, r, a, depth) => {
        let p = _polar(o, r, a)
        let a1 = a + 30 + _ra(), a2 = a - 30 + _ra()
        let r1 = r * k, r2 = r * k
        let p1 = _polar(p, r1, a1)
        let p2 = _polar(p, r2, a2)
        if (!result[depth]) {
            result[depth] = []
        }
        switch (treeCurve) {
            case 'QL':
                // 曲线1 QL
                result[depth].push([o, [...p, ...p1]], [p, p2])
                break;
            case 'QQ':
                // 曲线2 QQ
                result[depth].push([o, [...p, ...p1]], [o, [...p, ...p2]])
                break;
            case 'V':
                // 二叉树 V
                result[depth].push([o, p1], [o, p2])
                break
            default:
                // 二叉树 LL
                result[depth].push([o, p, p1], [p, p2])
        }

        // 
        // result.push([o, p], [p, p1], [p, p2])
        if (depth > 0) {
            _trer(p1, r1, a1, depth - 1)
            _trer(p2, r2, a2, depth - 1)
        }
    }
    if (n == 1) {
        _trer(translateY(o, r), r, a, depth)
    } else {
        Array.from({ length: n }).forEach((_, i) => {
            _trer(o, r, a + i * 360 / n, depth)
        })
    }

    return result.reverse()
}
// 二叉树
export const _tree = (options, props) => {
    let { depth = 5, color = "none",alpha=1 } = options
    let ps = treePoints(options)

    // 彩色
    if (color && color !== 'none') {
        let colors = _colors(color, depth + 1, alpha)
        let children = ps.map((t, i) => {
            return pointsPath({
                points: t,
                closed: false,
            }, {
                ...props,
                fill: 'none',
                stroke: colors[i],
            })
        })
        return _g(children)
    } else {
        // 单色
        return pointsPath({
            points: plainMatrix(ps),
            closed: false,
        }, {
            stroke: 'red',
            fill: 'none',
            ...props
        })
    }

}


export const _treeCircle = (options, props) => {
    let { depth = 5, color = "none" } = options
    let ps = treePoints(options)
    // 彩色
    if (color && color !== 'none') {
        let colors = _colors(color, depth + 1, 1)
        let children = ps.map((t, i) => {
            return pointsPath({
                points: t,
                closed: false,
            }, {
                ...props,
                fill: 'none',
                stroke: colors[i],
            })
        })
        return _g(children)
    } else {
        // 单色
        return pointsPath({
            points: plainMatrix(ps),
            closed: false,
        }, {
            stroke: 'red',
            fill: 'none',
            ...props
        })
    }

}